<template>
  <a-layout>
    <a-layout-sider width="200" style="background: #fff">
      <a-menu
              mode="inline"
              :style="{ height: '100%', borderRight: 0 }"
              @click="handleClick"
              :openKeys="openKeys"
      >
        <!-- 固定一个”欢迎“菜单，不能用a-sub-menu，因为它是一个组合菜单，要直接用a-menu-item -->
        <a-menu-item key="welcome">
          <MailOutlined/><span>欢迎</span>
        </a-menu-item>
        <a-sub-menu v-for="item in level1" :key="item.id" :disabled="true">
          <!-- 渲染一级菜单 -->
          <template #title>
              <span>
                <user-outlined />
                {{item.name}}
              </span>
          </template>
          <!-- 渲染二级菜单 -->
          <a-menu-item v-for="child in item.children" :key="child.id">
            <MailOutlined/><span>{{child.name}}</span>
          </a-menu-item>
        </a-sub-menu>
      </a-menu>
    </a-layout-sider>
    <a-layout-content
            :style="{ background: '#fff', padding: '24px', margin: 0, minHeight: '280px' }"
    >
      <div class="welcome" v-show="isShowWelcome">
        <the-welcome></the-welcome>
      </div>
      <a-list v-show="!isShowWelcome" item-layout="vertical" size="large" :grid="{ gutter: 20, column: 3 }" :data-source="ebooks">
        <template #renderItem="{ item }">
          <a-list-item key="item.name">
            <template #actions>
              <span>
                <component v-bind:is="'FileOutlined'" style="margin-right: 8px" />
                {{ item.docCount }}
              </span>
              <span>
                <component v-bind:is="'EyeOutlined'" style="margin-right: 8px" />
                {{ item.viewCount }}
              </span>
              <span>
                <component v-bind:is="'LikeOutlined'" style="margin-right: 8px" />
                {{ item.voteCount }}
              </span>
            </template>
            <a-list-item-meta :description="item.description">
              <!-- 标题 -->
              <template #title>
                <router-link :to="'/doc?ebookId=' + item.id">
                  {{ item.name }}
                </router-link>
              </template>
              <!-- 封面 -->
              <template #avatar>
                <a-avatar :src="baseURL+'/ebook/load-image/'+item.cover"/>
              </template>
            </a-list-item-meta>
          </a-list-item>
        </template>
      </a-list>
    </a-layout-content>
  </a-layout>
</template>

<script lang="ts">
import { defineComponent, onMounted, ref, reactive, toRef } from 'vue';
import axios from 'axios';
import { message } from 'ant-design-vue';
import {Tool} from "@/util/tool";
import TheWelcome from '@/components/the-welcome.vue';

export default defineComponent({
  name: 'Home',
  components: {
    TheWelcome,
  },
  setup() {
    const ebooks = ref(); //ref方式，实现数据绑定
//  const ebooks1 = reactive({books: []}); //reactive方式，实现数据绑定

    const openKeys = ref() ; // 当前展开的 SubMenu 菜单项 key 数组
    const level1 = ref() ;
    /** 查询所有分类 */
    const handleQueryCategory = () => {
        axios.get("/category/all").then((response) => {
            const data = response.data ;
            if(data.success){
                const categorys = data.content ;
                console.log("原始数组:" , categorys) ;

                //level1.value = [] ; // 先初始化成一个空数组，再给它赋值树形结构
                level1.value = Tool.array2Tree(categorys, 0) ; // 父id为0，表示根节点，即一级分类
                console.log("树形结构:" , level1.value) ;

                // 加载完分类后，将侧边菜单全部展开（只需要SubMenu的key，所有直接遍历level1了，没有遍历categorys）
                openKeys.value = [] ;
                for(let i=0; i<level1.value.length; i++){
                  openKeys.value.push(level1.value[i].id);
                }
            }else {
                message.error(data.message);
            }
        });
    };

    let category2Id = 0 ; // 定义二级分类变量

    /** 查询电子书列表 */
    const handleQueryEbook = () => {
      axios.get("/ebook/all", {
        params: {
          category2Id: category2Id
        }
      }).then((response) => {
        const data = response.data ;
        ebooks.value = data.content ; //ref方式，赋值
        //ebooks1.books = data.content ; //reactive方式，赋值
      });
    };

    const isShowWelcome = ref(true) ;

    const handleClick = (value: any) => {
      // 菜单点击 {key: '101', eventKey: 'menu_item_5_$$_101', keyPath: Array(2), eventKeyPath: Array(2), …}
      //console.log("菜单点击", value);
      if(value.key === 'welcome') {
        isShowWelcome.value = true ;
      }else {
        category2Id = value.key ;
        isShowWelcome.value = false ;
        handleQueryEbook() ;    // 查询电子书列表数据
      }
      //isShowWelcome.value = value.key === 'welcome' ;
    };

    onMounted(() => {
      // 查询所有分类，用于渲染左侧菜单
      handleQueryCategory() ;
      // 查询所有电子书，用于渲染右侧电子书列表（初始化欢迎页面，无需调用下面的方法）
      //handleQueryEbook() ;
    });

    return{
      ebooks,
//    ebooks2: toRef(ebooks1, "books"),
      level1,
      handleClick,
      isShowWelcome,
      baseURL : process.env.VUE_APP_SERVER,
      openKeys
    }
  }
});
</script>

<style scoped>
  .ant-avatar {
    width: 50px;
    height: 50px;
    line-height: 50px;
    border-radius: 8%;
    margin: 5px 0;
  }
</style>
